<template>
  <p class="g-name">{{detail.name}}</p>
  <p class="g-desc">{{detail.desc}}</p>
  <p class="g-price">
    <span>{{detail.price}}</span>
    <span>{{detail.oldPrice}}</span>
  </p>
  <div class="g-service">
    <dl>
      <dt>促销</dt>
      <dd>12月好物放送，App领券购买直降120元</dd>
    </dl>
    <dl>
      <dt>配送
      </dt>
      <dd>至 </dd>
      <XtxCity @change-city="changeCity"  :fullLocation='fullLocation'/>
    </dl>

    <dl>
      <dt>服务</dt>
      <dd>
        <span>无忧退货</span>
        <span>快速退款</span>
        <span>免费包邮</span>
        <a href="javascript:;">了解详情</a>
      </dd>
    </dl>
  </div>
</template>

<script>
import { toRef, ref } from 'vue'
export default {
  name: 'GoodName',
  props: {
    detail: {
      type: Object,
      default: () => ({})
    }
  },
  setup (props) {
    // 默认地址处理哈
    const userAddresses = toRef(props.detail, 'userAddresses')
    const provinceCode = ref('110000') // 省
    const cityCode = ref('119900') // 市
    const countyCode = ref('110101') // 区县
    const fullLocation = ref('北京市 市辖区 东城区') // 条件整体不成立就显示，也就是没有 登录之前显示的地址是这个哈
    if (userAddresses.value) {
      // 如果用户有默认地址就显示，没有就显示在地址中的第一个地址
      const defaultAddress = userAddresses.value.find(item => item.isDefault === 1) // 找出得就是默认的地址
      if (defaultAddress) { // 有默认地址
        provinceCode.value = defaultAddress.provinceCode
        cityCode.value = defaultAddress.cityCode
        countyCode.value = defaultAddress.countyCode
        fullLocation.value = defaultAddress.fullLocation
      } else { // 没有默认地址就显示用户地址里面的第一条地址
        provinceCode.value = userAddresses.value[0].provinceCode
        cityCode.value = userAddresses.value[0].cityCode
        countyCode.value = userAddresses.value[0].countyCode
        fullLocation.value = userAddresses.value[0].fullLocation
      }
    }
    // 更改城市信息哈
    const changeCity = (info) => {
      provinceCode.value = info.provinceCode
      cityCode.value = info.cityCode
      countyCode.value = info.countyCode
      fullLocation.value = info.fullLocation
    }
    return { fullLocation, changeCity }
  }
}
</script>

<style lang="less" scoped>
.g-name {
  font-size: 22px
}
.g-desc {
  color: #999;
  margin-top: 10px;
}
.g-price {
  margin-top: 10px;
  span {
    &::before {
      content: "¥";
      font-size: 14px;
    }
    &:first-child {
      color: @priceColor;
      margin-right: 10px;
      font-size: 22px;
    }
    &:last-child {
      color: #999;
      text-decoration: line-through;
      font-size: 16px;
    }
  }
}
.g-service {
  background: #f5f5f5;
  width: 500px;
  padding: 20px 10px 0 10px;
  margin-top: 10px;
  dl {
    padding-bottom: 20px;
    display: flex;
    align-items: center;
    dt {
      width: 50px;
      color: #999;
    }
    dd {
      color: #666;
      &:last-child {
        span {
          margin-right: 10px;
          &::before {
            content: "•";
            color: @xtxColor;
            margin-right: 2px;
          }
        }
        a {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
